package com.colin.aiyi.ui;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.colin.aiyi.R;
import com.colin.aiyi.app.Constant;
import com.colin.aiyi.dao.ImpxUtils.ImpRestPWForget;
import com.colin.aiyi.entity.ResetPWRequest;
import com.colin.aiyi.entity.response.BaseResponse;
import com.colin.aiyi.presenter.Imp_de.ImpPOther;
import com.colin.aiyi.ui.login.LoginAc;
import com.colin.aiyi.utils.TimeUtil;
import com.colin.aiyi.utils.VerifyUtil;
import com.colin.aiyi.view.ShowMsgUtil;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by hl on 2017/5/19.
 */

public class ResetPasswdAc extends BaseActivity implements View.OnClickListener, TimeUtil.TimeDownEvent {
    @BindView(R.id.img_logo)
    ImageView imgLogo;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.edit_vcode)
    EditText editVcode;
    @BindView(R.id.button_vcode)
    Button buttonVcode;
    @BindView(R.id.edit_phone1)
    EditText editPhone1;
    @BindView(R.id.edit_phone2)
    EditText editPhone2;
    @BindView(R.id.button_send)
    TextView buttonSend;
    @BindView(R.id.text_msg)
    TextView textMsg;
    @BindView(R.id.text_banner_left)
    TextView textBannerLeft;
    @BindView(R.id.text_banner_title)
    TextView textBannerTitle;
    @BindView(R.id.text_banner_rigth)
    TextView textBannerRigth;
    @BindView(R.id.linear)
    LinearLayout linear;
    private boolean flagTime = true;
    ImpPOther impPOther;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_reset_passwd);
        ButterKnife.bind(this);
        impPOther = new ImpPOther();
        linear.setBackgroundColor(Color.alpha(100));
        textBannerLeft.setOnClickListener(this);
        textBannerTitle.setText("");
    }

    @OnClick({R.id.button_vcode, R.id.button_send})
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.button_vcode:
                if (VerifyUtil.checkTextValueRight(new TextView[]{editPhone},
                        new String[]{"请输入正确手机号"},
                        new String[]{VerifyUtil.EX_PHONE},
                        textMsg)) {
                    impPOther.sendCode_forgetPw(editPhone.getText().toString().trim());
                    if (flagTime) {
                        TimeUtil.timeDown(Constant.TIMER, this);
                        flagTime = false;
                    }
                }
                break;
            case R.id.button_send:

                if (!VerifyUtil.checkTextValueRight(
                        new TextView[]{editPhone},
                        new String[]{"请输入正确手机号"},
                        new String[]{VerifyUtil.EX_PHONE},
                        textMsg)) {
                    return;
                }
                if (!checkEmpty(
                        new String[]{"请输入正确验证码", "请输入密码", "请输再次输入密码"},
                        new TextView[]{editVcode, editPhone1, editPhone2}
                        , textMsg
                )) {
                    return;
                }
                String pw1 = editPhone1.getText().toString();
                String pw2 = editPhone2.getText().toString();
                if (!pw1.equals(pw2)) {
                    textMsg.setText("两次输入密码不一致");
                    return;
                }
                ResetPWRequest request = new ResetPWRequest();
                ImpRestPWForget impRestPWForget = new ImpRestPWForget();
                request.setTelNo(editPhone.getText().toString());
                request.setMessageCode(editVcode.getText().toString());
                request.setCode(editVcode.getText().toString());
                request.setPassword(pw1);
                impRestPWForget.getObject(request);
                ShowMsgUtil.showLoadingDialog(this);
                break;
        }
    }

    @Subscribe()
    public void msgProcesse(Object object) {
        ShowMsgUtil.hideLoadingDialog();
        BaseResponse baseResponse = (BaseResponse) object;
        switch (baseResponse.getMsgType()) {
            case 5:
                if (baseResponse.getCode() == 200) {
                    showMsg("短信已发送到" + editPhone.getText().toString());
                } else {
                    showMsg(baseResponse.getMessage());
                }

                break;
            case 6:
                if (baseResponse.getCode() == 200) {
                    showMsg("重置密码成功");
                    Intent intent = new Intent(this, LoginAc.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    finish();
                } else {
                    showMsg(baseResponse.getMessage());
                }

                break;
        }
    }

    @Override
    public void timeDown(int indexTime) {
        if (indexTime == -1) {
            flagTime = true;
            buttonVcode.setText("发送");
            return;
        }
        indexTime = Constant.TIMER - indexTime;
        indexTime = indexTime / 1000;
        buttonVcode.setText(indexTime + "秒后重试");
    }
}
